<?php
	//$lifeTime = 36;  
	//session_set_cookie_params($lifeTime);
	session_start(); 
    class reflectIndex{
	
		var  $tpl;
		var $con;
		var $username;
		public  function reflect()
		{
			include("../smarty/main.php"); 
			$this->tpl=$tpl;
			include("../connectMysql/connetMysql.class.php");
			   
			
			
			//$this->tpl=$tpl;
			$conClass=new connetMysql;
			$con=$conClass->getConnect();
			$this->con=$con;
			mysqli_query($con,"SET names utf8");
			
			
			
			
			
			
			$sql="SELECT * FROM newstype ";
			$result = mysqli_query($con,$sql);
			$bankuai="";
			//插入板块
			while($row = mysqli_fetch_array($result))
			{//<li><a href="#">体育</a></li>
			 global $bankuai; 
			 $bankuai=$bankuai."<li ><a href=\"#\">".$row["newstypename"]."<font color=\"#F00\"  id=\"banKuan_".$row["newstypeid"]."\"></font></a></li>";
			// echo $bankuai; 
			 //$bankuai="newstypename";
			//$tpl->assign("bankuai",'<li><a href="#">$row["newstypename"]</a></li>');
			}
			//echo $bankuai;
			$tpl->assign("bankuai",$bankuai);
			//***插入板块**//////
			//$tpl->display("indexMuBan.html");
			
			
			//**头条板块查询
			$sql="select *from newsdraft where newsheadlines=1 and newsstate=1 order by newssubmittime DESC limit 0,7";
			$resultTouTiao = mysqli_query($con,$sql);
			//**头条查询**//
			$sqlNotToutiao="select *from newsdraft where newsheadlines=0 and newsstate=1 order by newssubmittime DESC limit 0,22 ";
			$resultNotToutiao = mysqli_query($con,$sqlNotToutiao);
			$touTiao="";
			$count=4;
			while($count>0)//前面4条头条
			{  //<h2><a href="#">本届政治局集体学习首次聚焦反腐形势</a></h2>
			   // <p><a href="#">习近平:坚决查处违纪违法者</a></p>
			   $count--;
			   $rowTouTiao = mysqli_fetch_array($resultTouTiao);
			   $rowNotTouTiao=mysqli_fetch_array($resultNotToutiao);//非头条结果
				global $touTiao;
				$touTiao=$touTiao."<h2><a href=\"../display/display_news.php?newsid=".$rowTouTiao['newsid']."\">".$rowTouTiao['newstitle']."</a></h2>";
				$touTiao=$touTiao."<p><a href=\"../display/display_news.php?newsid=".$rowNotTouTiao['newsid']."\">".$rowNotTouTiao['newstitle']."</a></p>";
			}
			
			
			//$count=6;
			for($i=0;$i<3;$i++)
			{
				$rowTouTiao = mysqli_fetch_array($resultTouTiao);//第五条头条
				$touTiao=$touTiao."<h2><a href=\"../display/display_news.php?newsid=".$rowTouTiao['newsid']."\">".$rowTouTiao['newstitle']."</a></h2>";
				$touTiao=$touTiao."<ul>";
				for($j=0;$j<6;$j++){
					 $rowNotTouTiao=mysqli_fetch_array($resultNotToutiao);//非头条结果
					 $touTiao=$touTiao."<li><a href=\"../display/display_news.php?newsid=".$rowNotTouTiao['newsid']."\">".$rowNotTouTiao['newstitle']."</a></li>";
				}
				$touTiao=$touTiao."</ul>";
			}
			
			
			$tpl->assign("newsheadlines",$touTiao);
			
			//
			
			//$tpl->display("indexMuBan.html");
			//$contect = $tpl->fetch("indexMuBan.html");  
			//$fp = fopen("language/zh/index.php", "w");  
			//fwrite($fp, $contect);  
			//fclose($fp); 
		} 
		public  function login()
		{
			//global $tpl;
			//if($_COOKIE['language']==null)
				//$mathLangMuban="Location:language/zh/";
			//else
				//$mathLangMuban="Location:language/".$_COOKIE['language']."/";
			if(isset($_COOKIE['user']))
			{
				 //<li><a href="#">登陆</a></li>
           		// <li><a href="#">注册</a></li>
				 $_SESSION['user']=$_COOKIE['user'];
				$islogin="<li><a href=\"#\">".$_COOKIE['user']."</a></li><li><a href=\"#\">退出</a></li>";
				$this->tpl->assign("login",$islogin);
			}
			else if(isset($_SESSION['user']))
			{
				$this->genxinGuanzhu($_SESSION['user']);
				$islogin="<li><a href=\"#\">".$_SESSION['user']."</a></li><li><a href=\"#\">退出</a></li>";
				$this->tpl->assign("login",$islogin);
			}
			else
			{
				$islogin="<li><a href=\"../login/login.html\">登录</a></li><li><a href=\"../registertest/register/register.html\">注册</a></li>";
				$this->tpl->assign("login",$islogin);
			}
			
		}
		public function genxinGuanzhu( $user)//查询关注新闻类型  判断登录后才调用
		{
			//$this->con
			$sql="SELECT * from `user`,attention,newstype  WHERE username='$user' AND newstypeid=attentionnewstype";
			//echo $sql;
			$result = mysqli_query($this->con,$sql);
			$str="";
			while($row= mysqli_fetch_array($result))
			{
				// window.setInterval(function(){
							 //	$.get("${pageContext.request.contextPath}/communication/user/ajax.mvc",
									// {"timed": new Date().getTime()},
									// function (data)
									// {
										// $("#logs").append("[data: " + data + " ]<br/>");
										//document.getElementById("msg").innerHTML="此用户名可用";

									// });
							// },3000);//3秒刷新一次
					$str=$str."window.setInterval(function(){\$.post(\"checkUpdata/checkUpdata.php\",
					{\"attentionnewstype\":". $row["attentionnewstype"]."},function (data)
					{  // var count=data;
						if(data>=99){
						document.getElementById(\"banKuan_".$row["attentionnewstype"]."\").innerHTML=\"(99+)\";
						
						}
						else if(data>0)
						{
							//data=data.replace(/(^\s+)|(\s+$)/g,\"\");
							data=data.replace(/(^\s+)|(\s+$)/g,\"\");
							document.getElementById(\"banKuan_".$row["attentionnewstype"]."\").innerHTML=\"(\"+data+\")\";
						}
					
					});},3000);";
					
					
			}
			$this->tpl->assign("guanzhu",$str);
			
		}
		
		public function rukou()//入口函数调用其他所有函数
		{
			$this->reflect();
			$this->login();
			
			$indexPage="language/zh/index.html";
			if(isset($_SESSION['language']))
			{
				$indexPage="language/".$_SESSION['language']."/index.html";
			}
			$this->tpl->display($indexPage);
		}
		

	}
	
	
	$re=new reflectIndex;
	$re->rukou();
	//$re->login();
?>